// pages/order-detail/order-detail.js
const { apiService } = require('../../utils/api')
const { showSuccess, showError, formatPrice } = require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: null,
    order: null,
    loading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        orderId: options.id
      })
      this.loadOrderDetail(options.id)
    } else {
      showError('订单ID不能为空')
      wx.navigateBack()
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.loadOrderDetail(this.data.orderId)
    wx.stopPullDownRefresh()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '订单详情',
      path: `/pages/order-detail/order-detail?id=${this.data.orderId}`
    }
  },

  /**
   * 加载订单详情
   */
  async loadOrderDetail(orderId) {
    try {
      this.setData({ loading: true })
      const order = await apiService.getOrderDetail(orderId)
      this.setData({
        order: order,
        loading: false
      })
    } catch (error) {
      this.setData({ loading: false })
      showError(error.message || '加载订单详情失败')
    }
  },

  /**
   * 取消订单
   */
  async onCancelOrder() {
    try {
      wx.showModal({
        title: '取消订单',
        content: '确定要取消这个订单吗？',
        success: async (res) => {
          if (res.confirm) {
            await apiService.cancelOrder(this.data.orderId)
            showSuccess('订单取消成功')
            this.loadOrderDetail(this.data.orderId)
          }
        }
      })
    } catch (error) {
      showError(error.message || '取消订单失败')
    }
  },

  /**
   * 支付订单
   */
  async onPayOrder() {
    try {
      await apiService.payOrder(this.data.orderId)
      showSuccess('支付成功')
      this.loadOrderDetail(this.data.orderId)
    } catch (error) {
      showError(error.message || '支付失败')
    }
  },

  /**
   * 确认收货
   */
  async onConfirmOrder() {
    try {
      wx.showModal({
        title: '确认收货',
        content: '确认已收到商品吗？',
        success: async (res) => {
          if (res.confirm) {
            await apiService.confirmOrder(this.data.orderId)
            showSuccess('确认收货成功')
            this.loadOrderDetail(this.data.orderId)
          }
        }
      })
    } catch (error) {
      showError(error.message || '确认收货失败')
    }
  },

  /**
   * 申请退款
   */
  async onRefundOrder() {
    try {
      wx.showModal({
        title: '申请退款',
        content: '确定要申请退款吗？',
        success: async (res) => {
          if (res.confirm) {
            await apiService.refundOrder(this.data.orderId)
            showSuccess('退款申请已提交')
            this.loadOrderDetail(this.data.orderId)
          }
        }
      })
    } catch (error) {
      showError(error.message || '申请退款失败')
    }
  },

  /**
   * 复制订单号
   */
  onCopyOrderNumber() {
    wx.setClipboardData({
      data: this.data.order.orderNumber,
      success: function() {
        showSuccess('订单号已复制')
      }
    })
  },

  /**
   * 联系客服
   */
  onContactService() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567',
      fail: function() {
        showError('拨号失败，请手动联系客服')
      }
    })
  },

  /**
   * 获取状态文本
   */
  getStatusText(status) {
    const statusMap = {
      1: '待支付',
      2: '已支付',
      3: '已发货',
      4: '已完成',
      5: '已取消',
      6: '已退款'
    }
    return statusMap[status] || '未知状态'
  },

  /**
   * 获取状态样式类
   */
  getStatusClass(status) {
    const classMap = {
      1: 'status-pending',
      2: 'status-paid',
      3: 'status-delivered',
      4: 'status-completed',
      5: 'status-cancelled',
      6: 'status-refunded'
    }
    return classMap[status] || ''
  }
})